package com.chushouya.order.service.api.impl;

import com.alipay.api.domain.AlipayOpenMiniOrderDeliveryReceiveModel;
import com.alipay.api.domain.DeliveryReceiveDTO;
import com.chushouya.manager.service.common.AlipayClientCommonService;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.entity.OrderExtendEntity;
import com.chushouya.order.dao.entity.OrderLogisticsEntity;
import com.chushouya.order.dao.repository.OrderExtendRepository;
import com.chushouya.order.dao.repository.OrderLogisticsRepository;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.dto.admin.orderlogistics.OrderLogisticsQuery;
import com.chushouya.order.service.api.AlipayOpenMiniOrderDeliveryReceiveService;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class AlipayOpenMiniOrderDeliveryReceiveServiceImpl implements AlipayOpenMiniOrderDeliveryReceiveService {

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private OrderExtendRepository orderExtendRepository;

    @Resource
    private AlipayClientCommonService alipayClientCommonService;

    @Resource
    private OrderLogisticsRepository orderLogisticsRepository;

    /**
     * 获取订单信息
     * @param orderId
     */
    private OrderEntity getOrder(Long orderId) {
        OrderEntity orderEntity = orderRepository.selectByPrimaryKey(orderId);
        if (Objects.isNull(orderEntity)) {
            throw new RuntimeException("订单不存在");
        }
        return orderEntity;
    }

    /**
     * 订单额外信息
     * @param orderId
     */
    private OrderExtendEntity getOrderExtend(Long orderId) {
        OrderExtendEntity orderExtendEntity = orderExtendRepository.selectByOrderId(orderId);
        if (Objects.isNull(orderExtendEntity)) {
            throw Ex.business("订单扩展信息不存在");
        }
        return orderExtendEntity;
    }

    /**
     * 获取订单快递单号
     * @param orderId
     */
    private String getExpressNo(Long orderId) {
        OrderLogisticsQuery query = new OrderLogisticsQuery();
        query.setOrderId(orderId);
        OrderLogisticsEntity orderLogisticsEntity = orderLogisticsRepository.selectOne(query);
        if (Objects.isNull(orderLogisticsEntity)) {
            throw Ex.business("订单物流信息不存在");
        }
        return orderLogisticsEntity.getExpressNo();
    }

    @Override
    public void orderDeliveryReceive(Long orderId) {
        OrderEntity orderEntity = getOrder(orderId);
        OrderExtendEntity orderExtendEntity = getOrderExtend(orderId);
        AlipayOpenMiniOrderDeliveryReceiveModel model = new AlipayOpenMiniOrderDeliveryReceiveModel();
//        List<DeliveryReceiveDTO> deliveryReceiveList = Lists.newArrayList();
//        DeliveryReceiveDTO deliveryReceiveDTO = new DeliveryReceiveDTO();
//        deliveryReceiveDTO.setWaybillId(getExpressNo(orderId));
//        deliveryReceiveList.add(deliveryReceiveDTO);
        model.setOrderId(orderEntity.getOrderCenterNo());
        model.setOutOrderId(orderEntity.getOrderNo());
        model.setUserId(orderExtendEntity.getOpenId());
//        model.setDeliveryReceiveList(deliveryReceiveList);
        alipayClientCommonService.alipayOpenMiniOrderDeliveryReceive(model);
    }

}
